package yunhe.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import yunhe.biz.StockMgrBiz;
import yunhe.biz.impl.StockMgrBizImpl;
import yunhe.entity.Product;
import yunhe.entity.StockInAppend;
import yunhe.entity.StockOutAppend;

@WebServlet(name = "GetAllStockInOutServlet",urlPatterns = "/GetAllStockInOutServlet")
public class GetAllStockInOutServlet extends HttpServlet
{
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException
{
   
  StockMgrBiz stock=new StockMgrBizImpl();
	String date1=req.getParameter("date1");
	String date2=req.getParameter("date2");
	String xuanze=req.getParameter("xuanze");
   
	if(xuanze==null||xuanze.equals(""))
	{
		xuanze="1";
	}
	if(xuanze.equals("1"))
	{
		Object[] list=stock.queryStockOutBydate(date1, date2,xuanze);
		List<StockInAppend> StockIn=(List<yunhe.entity.StockInAppend>) list[0];
		List<Product> pro=(List<Product>) list[1];
		JSONArray ja=new JSONArray();
		for(int i=0;i<StockIn.size();i++)
		{
			   Map<String,String> mss=new HashMap<>();
			   mss.put("stockInsId",StockIn.get(i).getStockIn().getStockInsId());
			   mss.put("stockInQuantity", Integer.toString(StockIn.get(i).getStockInQuantity()));
			   mss.put("pursId", StockIn.get(i).getPursId());
			   mss.put("stockInReason", StockIn.get(i).getStockIn().getStockInReason());
			   mss.put("proBarCode",pro.get(i).getProBarCode());
			   mss.put("proName", pro.get(i).getProName());
			   mss.put("proQual", pro.get(i).getProQual());
			   mss.put("proUnit", pro.get(i).getProUnit());
			   mss.put("stockdate",StockIn.get(i).getStockIn().getStockInDate().toString());
			   ja.put(mss);
		}
		resp.getWriter().print(ja.toString());
		
	}
	else if(xuanze.equals("2"))
	{
		
		Object[] list=stock.queryStockOutBydate(date1, date2,xuanze);
		List<StockOutAppend> StockOut=(List<yunhe.entity.StockOutAppend>) list[0];
		List<Product> pro=(List<Product>) list[1];
		JSONArray ja=new JSONArray();
		for(int i=0;i<StockOut.size();i++)
		{
			   Map<String,String> mss=new HashMap<>();
			   mss.put("stockInsId",StockOut.get(i).getStockOut().getStockOutsId());
			   mss.put("stockInQuantity", Integer.toString(StockOut.get(i).getStockOutQuantity()));
			   mss.put("pursId", StockOut.get(i).getStoId());
			   mss.put("stockInReason", StockOut.get(i).getStockOut().getStockReason());
			   mss.put("proBarCode",pro.get(i).getProBarCode());
			   mss.put("proName", pro.get(i).getProName());
			   mss.put("proQual", pro.get(i).getProQual());
			   mss.put("proUnit", pro.get(i).getProUnit());
			   mss.put("stockdate",StockOut.get(i).getStockOut().getStockOutDate().toString());
			   ja.put(mss);
		}
		resp.getWriter().print(ja.toString());	
	}
	
	
	
	

}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
	  doGet(req,resp);
	}
}
